"use strict";

function BaseFilter() {
}
module.exports = BaseFilter;

BaseFilter.prototype.init = function (acceptList, rejectList) {
  this.acceptList = acceptList;
  this.rejectList = rejectList;
};
/**
 * 过滤函数 不要 修改or override这个函数
 * 拷贝输入对象,进行变换,然后进行检测
 * @param srcText,不要对这个字段做任何修改
 * @returns {boolean} true表示通过过滤(可以继续往下走), false表示未通过(这个词被屏蔽)
 */
BaseFilter.prototype.filter = function (srcText) {
  var transTxt = this.__transform(srcText);
  var isPassed = this.__innerFilter(transTxt);
  if (this.__isDebug) {
    //console.log(this.constructor.name + ', ' + srcText + ', isPassed=' + isPassed);
    if (isPassed) {
      this.__passed.push(srcText);
    } else {
      this.__blocked.push(srcText);
    }
  }
  return isPassed;
};

/**
 * 查看过滤细节,并进行清理(debug mode)
 */
BaseFilter.prototype.showAndClean = function () {
  if (!this.__isDebug) {
    console.log('BaseFilter not in debug mode');
    return;
  }
  console.log('----' + this.constructor.name);
  console.log(' pass:', this.__passed.join(','));
  this.__passed = [];
  console.log(' block:', this.__blocked.join(','));
  this.__blocked = [];
};

/**
 * 对 str进行变换,需要被覆盖
 * @param str
 * @returns {*}
 */
BaseFilter.prototype.__transform = function (str) {
  return str;
};

/**
 * 通用过滤
 * @param str
 * @returns {boolean}
 */
BaseFilter.prototype.__innerFilter = function (str) {
  if (this.acceptList && this.acceptList.indexOf(str) >= 0) {
    return true;
  }
  if (this.rejectList && this.rejectList.indexOf(str) >= 0) {
    return false;
  }
  // 其他策略
  return this.detailFilter(str);
};

/**
 * 一般过滤,需要被覆盖
 * @param str
 * @returns {boolean} true表示通过过滤(可以继续往下走), false表示未通过(这个词被屏蔽)
 */
BaseFilter.prototype.detailFilter = function (str) {
  return true;
};

/**
 * 测试脚本
 * @param sampleArr
 * @private
 */
BaseFilter.prototype.__testScript = function (sampleArr) {
  //var sample = ["一个", "一百年", "一千万个", "一颗颗"];
  //var target = new FilterQuantifier(true);
  var self = this;
  sampleArr.forEach(function (item) {
    self.filter(item);
  });
  console.log('sample:', sampleArr.join(','));
  self.showAndClean();
};

